/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using ELE.EnterpriseLibrary.Data.Configuration;
using ELE.EnterpriseLibrary.Properties;

namespace ELE.EnterpriseLibrary.Data
{
  /// <summary>
  /// A view of configuration settings of Object Sql Mappings
  /// </summary>
  public class ObjectSqlMappingConfigurationView
  {
    private IConfigurationSource configurationSource;
    private QueryBuilderData defaultQueryBuilder;
    /// <summary>
    /// Builds a view from a configuration source.
    /// </summary>
    /// <param name="configuration"></param>
    public ObjectSqlMappingConfigurationView(IConfigurationSource configuration)
    {
      this.configurationSource = configuration;

      ObjectMappingSettings settings = this.ObjectMappingSettings;
      foreach (QueryBuilderData qbData in settings.QueryBuilders)
      {
        if (qbData.Default)
        {
          this.defaultQueryBuilder = qbData;
          break;
        }
      }
    }
    /// <summary>
    /// Get mapping data by name.
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public MappingQueryData GetMappingQueryData(string name)
    {
      ObjectMappingSettings settings = this.ObjectMappingSettings;
      if (!settings.Mappings.Contains(name))
      {
        throw new ConfigurationErrorsException(string.Format(Resources.Culture, Resources.ExceptionMappingNotFound, name));
      }
      return settings.Mappings.Get(name);
    }
    /// <summary>
    /// Get query data by name.
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public SqlQueryData GetSqlQueryData(string name)
    {
      ObjectMappingSettings settings = this.ObjectMappingSettings;
      if (!settings.Queries.Contains(name))
      {
        throw new ConfigurationErrorsException(string.Format(Resources.Culture, Resources.ExceptionSqlQueryNotFound, name));
      }
      return settings.Queries.Get(name);
    }
    /// <summary>
    /// Get object definition by name.
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public TypeData GetObjectQueryData(string name)
    {
      ObjectMappingSettings settings = this.ObjectMappingSettings;
      if (!settings.Objects.Types.Contains(name))
      {
        throw new ConfigurationErrorsException(string.Format(Resources.Culture, Resources.ExceptionObjectTypeNotFound, name));
      }
      return settings.Objects.Types.Get(name);
    }
    /// <summary>
    /// get assembly definition by name.
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public AssemblyData GetAssemblyQueryData(string name)
    {
      ObjectMappingSettings settings = this.ObjectMappingSettings;
      if (!settings.Objects.Assemblies.Contains(name))
      {
        throw new ConfigurationErrorsException(string.Format(Resources.Culture, Resources.ExceptionObjectTypeNotFound, name));
      }
      return settings.Objects.Assemblies.Get(name);
    }
    /// <summary>
    /// Get query builder by name.
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public QueryBuilderData GetQueryBuilderData(string name)
    {
      if (string.IsNullOrEmpty(name))
      {
        if (this.defaultQueryBuilder == null)
        {
          throw new ConfigurationErrorsException(string.Format(Resources.Culture, Resources.ExceptionQueryBuilderNotFound, "*default query builder*"));
        }
        return this.defaultQueryBuilder;
      }
      ObjectMappingSettings settings = this.ObjectMappingSettings;
      if (!settings.QueryBuilders.Contains(name))
      {
        throw new ConfigurationErrorsException(string.Format(Resources.Culture, Resources.ExceptionQueryBuilderNotFound, name));
      }
      return settings.QueryBuilders.Get(name);
    }

    /// <summary>
    /// Gets the name of the default <see cref="ObjectMappingData"/>.
    /// </summary>
    /// <returns>
    /// The name of the default <see cref="ObjectMappingData"/>.
    /// </returns>
    public string DefaultObjectSqlMappingManager
    {
      get
      {
        ObjectMappingSettings configSettings = this.ObjectMappingSettings;
        return configSettings.DefaultMapping;
      }
    }

    /// <summary>
    /// Gets the <see cref="ObjectMappingSettings"/> configuration data.
    /// </summary>
    /// <returns>
    /// The <see cref="ObjectMappingSettings"/> configuration data.
    /// </returns>
    public ObjectMappingSettings ObjectMappingSettings
    {
      get { return (ObjectMappingSettings)configurationSource.GetSection(ObjectMappingSettings.SectionName); }
    }
  }
}
